package ClasesDAO;

import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import tp.disenio.factory.JpaUtil;
import tp.disenio.shared.Participante;


public class ParticipanteDAO {
	
	
	/**
	 * 
	 * @param id_competencia
	 * @param nombre
	 * @return
	 */
	public static boolean existeParticipante(int idCompetencia, String nombre) {
		
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("auslober",System.getProperties());
		EntityManager em = factory.createEntityManager();
		
		em.getTransaction().begin();
		
			Query query = em.createQuery("SELECT P.nombre FROM Participante P WHERE P.competencia.idCompetencia = " + idCompetencia + " AND P.nombre = \"" + nombre + "\"");
			ArrayList<String> camposString = new ArrayList<String>(query.getResultList());
			
		em.getTransaction().commit();
		
		if (camposString.size() > 0){
			return true;
		}		
				
		return false;
	}
	
	/**
	 * 
	 * @param id_competencia
	 * @param nombre
	 * @return
	 */
	public static boolean existeParticipanteMail(int idCompetencia, String mail) {
		
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("auslober",System.getProperties());
		EntityManager em = factory.createEntityManager();
		
		em.getTransaction().begin();
		
			Query query = em.createQuery("SELECT P.nombre FROM Participante P WHERE P.competencia.idCompetencia = " + idCompetencia + " AND P.correoElectronico = \"" + mail + "\"");
			ArrayList<String> camposString = new ArrayList<String>(query.getResultList());
			
		em.getTransaction().commit();
		
		if (camposString.size() > 0){
			return true;
		}		
				
		return false;
	}
	
	
	/**
	 * 
	 * @param participante
	 */
	public static void guardarParticipante(Participante participante) {
				
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("auslober",System.getProperties());
		EntityManager em = factory.createEntityManager();
		
		em.getTransaction().begin();
			
			em.persist(participante);
			em.flush();
			
		em.getTransaction().commit();	
		
	}
}
